<?php

set_include_path(get_include_path() . PATH_SEPARATOR .
        '..' . PATH_SEPARATOR .
        dirname(dirname(__FILE__)));

require_once 'components/utils/debug_utils.php';

Warning::BeginSuppress(PHPWarning::$Deprecated);
require_once 'simpletest/autorun.php';
require_once 'simpletest/mock_objects.php';
Warning::EndSuppress();

require_once 'components/renderer.php';
require_once 'components/input_values.php';
require_once 'components/vertical_grid_processor.php';
require_once 'components/vertical_grid.php';
require_once 'components/utils/string_utils.php';

require_once 'database_engine/common_engine.php';
require_once 'database_engine/dataset.php';

Mock::generate('IDataset');
Mock::generate('VerticalGrid');


class VerticalGridProcessor01 extends VerticalGridProcessor
{
    private $dataset;
    private $grid;

    public function __construct($name, Renderer $renderer, IDataset $dataset = null, VerticalGrid $grid = null)
    {
        parent::__construct($name, $renderer);
        $this->dataset = $dataset;
        $this->grid = $grid;
    }

    protected function DoCreateDataset()
    {
        if (isset($this->dataset))
        {
            return $this->dataset;
        }
        else
        {
            return new MockIDataset();
        }
    }

    protected function DoCreateComponent()
    {
        if (isset($this->grid))
        {
            return $this->grid;
        }
        else
        {
            return new MockVerticalGrid();
        }
    }
}

Mock::generate('Renderer');

class VerticalGridProcessorTests extends UnitTestCase
{
    /** @var SimpleMock|Renderer */
    private $renderer;

    public function testIsCreated()
    {
        $processor = new VerticalGridProcessor01('vertical_grid_01', $this->renderer);

        $this->assertFalse($processor->IsCreated());
    }

    public function setUp()
    {
        $this->renderer = new MockRenderer(); 
    }

    public function testSimpleProcess()
    {
        $processor = new VerticalGridProcessor01('vertical_grid_01', $this->renderer);
        
        $processor->AddOperation(
            new UpdateGridOperation('u01', array('[id]'))
        );
        $processor->AddOperation(
            new UpdateGridOperation('u02', array('[other_id]'))
        );

        $input1 = new PageInputStub(
            array(
                'c' => 'vertical_grid_01',
                'a' => 'update',
                'sa' => 'u01',
                'pk0' => 1
            ),
            array()
        );

        $processor->ProcessInput($input1);

        $this->assertTrue($processor->IsCreated());

        /** @var SimpleMock|EditableDataset $dataset */
        $dataset = $processor->GetDataset();

        $dataset->expectAt(0, 'AddSimpleCondition', array('[id]', ConditionOperator::$Equals, 1));
        $dataset->expectAtLeastOnce('Open');
        $processor->GetVerticalGrid()->expectAtLeastOnce('RenderGridContent');
        $dataset->expectAtLeastOnce('Close');

        $input2 = new PageInputStub(
            array(
                'c' => 'vertical_grid_01',
                'a' => 'update',
                'sa' => 'u02',
                'pk0' => 1
            ),
            array()
        );

        $response = $processor->ProcessInput($input2);

        $this->assertNotNull($response);

        $this->assertEqual(
            '<?xml version="1.0" encoding="UTF-8"?><response><type><![CDATA[OK]]></type><content><![CDATA[]]></content></response>',
            StringUtils::RemoveNewLines($response->GetAsText()));

        /** @var SimpleMock|IDataset $dataset */
        $dataset = $processor->GetDataset();

        $dataset->expectAt(1, 'AddSimpleCondition', array('[other_id]', ConditionOperator::$Equals, 1));
        $dataset->expectAtLeastOnce('Open');
        $processor->GetVerticalGrid()->expectAtLeastOnce('RenderGridContent');
        $dataset->expectAtLeastOnce('Close');

        $dataset->expectCallCount('AddSimpleCondition', 2);
    }

    
    public function testSimpleInsert()
    {
        $processor = new VerticalGridProcessor01('vertical_grid_01', $this->renderer);
        $processor->AddOperation(new CommitInsertOperation());

        $input = new PageInputStub(
            array(
                'c' => 'vertical_grid_01',
                'a' => 'post',
                'sa' => 'insert'
            ),
            array(
                'id_edit' => '1',
                'value_edit' => 'string value'
            )
        );

        $response = $processor->ProcessInput($input);

        $this->assertNotNull($response);
        $this->assertEqual(
            '<?xml version="1.0" encoding="UTF-8"?><response><type><![CDATA[OK]]></type></response>',
            StringUtils::RemoveNewLines($response->GetAsText()));

        /** @var SimpleMock|VerticalGrid $grid  */
        $grid = $processor->GetVerticalGrid();
        /** @var SimpleMock|IDataset $dataset */
        $dataset = $processor->GetDataset();

        $dataset->expectOnce('Insert');
        $grid->expectOnce('ProcessPost');
        $dataset->expectOnce('Post');
    }

    public function testSimpleEdit()
    {
        /** @var SimpleMock|IDataset $dataset */
        $dataset = new MockIDataset();
        /** @var SimpleMock|VerticalGrid $grid  */
        $grid = new MockVerticalGrid();

        $grid->setReturnValue('ExtractPrimaryKeyValuesFromPost', array("id" => 1));

        $processor = new VerticalGridProcessor01('vertical_grid_01', $this->renderer, $dataset, $grid);
        $processor->AddOperation(new CommitUpdateOperation());

        $input = new PageInputStub(
            array(
                'c' => 'vertical_grid_01',
                'a' => 'post',
                'sa' => 'update'
            ),
            array(
                'pk0' => '1',
                'id_edit' => '2',
                'value_edit' => 'string value'
            )
        );

        $dataset->setReturnValue('GetFieldNames', array("id", "value"));
        $dataset->setReturnValue('IsPrimaryKeyField', true, array("id"));
        $dataset->setReturnValue('IsPrimaryKeyField', false, array("value"));

        $response = $processor->ProcessInput($input);
        $this->assertEqual(
            '<?xml version="1.0" encoding="UTF-8"?><response><type><![CDATA[OK]]></type></response>',
            StringUtils::RemoveNewLines($response->GetAsText()));

        $this->assertNotNull($response);

        $dataset->expectOnce('AddSimpleCondition', array('id', ConditionOperator::$Equals, 1));
        $dataset->expectOnce('Open');
        $dataset->expectOnce('Edit');
        $grid->expectOnce('ProcessPost');
        $dataset->expectOnce('Post');
        $dataset->expectOnce('Close');
    }
}

?>